#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=30;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char a[maxn],b[maxn],ans[maxn];
bool vis[maxn];
LL sum=0;
LL check(LL n){
    LL res=0;
    for(LL i=1;i<=n;i++){
        res=res*10+(ans[i]-'0');
    }
    return res;
}
void dfs(LL j,LL len,LL n,bool flag){

    if(len>n){
        sum=max(sum,check(n));
        return;
    }
    if(flag==1){
        for(LL i=1;i<=n;i++){
            if(!vis[i]&&a[i]<=b[j]){
                vis[i]=1;
                ans[j]=a[i];
                if(a[i]==b[j]){
                    dfs(j+1,len+1,n,flag);
                    vis[i]=0;
                }
                else{
                    dfs(j+1,len+1,n,flag^1);
                    vis[i]=0;
                }
            }
        }
    }
    if(flag==0){
        for(LL i=n;i>=1;i--){
            if(!vis[i]){
                vis[i]=1;
                ans[j]=a[i];
                dfs(j+1,len+1,n,flag);
                vis[i]=0;
            }
        }
    }
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>(a+1);LL n=strlen(a+1);
  cin>>(b+1);LL m=strlen(b+1);
  sort(a+1,a+1+n);
  if(n<m){
    for(LL i=n;i>=1;i--) cout<<a[i];
    cout<<"\n";
  }
  else {dfs(1,1,n,1); cout<<sum<<"\n";}
return 0;
}
